package net.atomtree.corrsepond.factory;

import net.atomtree.corrsepond.template.ApplicationURLTemplate;
import net.atomtree.corrsepond.template.JobURLTemplate;
import net.atomtree.corrsepond.template.StageURLTemplate;
import net.atomtree.handler.HTTPRequestPoster;
import java.util.Map;

/**
 * Created by czk on 2016/1/2.
 */
public class SparkRestFactory implements CorrespondByRest,CorrespondProperty {

    /**
     * you can use url template to get you need rest url
     * support access application,stage,job three kind of rest url at present
     * @param URLParam the template need one or more param to consist a rest url
     * @param correspondObject the correspond Object represent you access role such as application stage job task...
     * @return rest url
     */
    @Override
    public String URLFactory(Map<String, String> URLParam,int correspondObject) throws Exception {
        if (correspondObject==APPLICATION){
            String applicationURL=new ApplicationURLTemplate().analyse(URLParam);
            return applicationURL;
        }
        if(correspondObject==STAGE){
            String stageURL=new StageURLTemplate().analyse(URLParam);
            return stageURL;
        }
        if(correspondObject==JOB) {
            String jobURL = new JobURLTemplate().analyse(URLParam);
            return jobURL;
        }
        throw new Exception("please input correct correspond such as APPLICATION,STAGE,JOB NUMBER");
    }

    /**
     * correspond spark monitor with rest
     * @param URL correspond url
     * @return rest result like jsonArray string
     * @throws Exception
     */
    @Override
    public String Correspond(String URL)throws Exception{
        String RESTWithSparkResult=HTTPRequestPoster.sendGetRequest(URL);
        return RESTWithSparkResult;
    }

}
